﻿using ET_DbCommon.Model;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ET_DbCommon.DAL
{
    /// <summary>
    /// 试卷表数据库操作类
    /// </summary>
    public static class PaperDAL
    {
        public static int Insert(PaperModel model)
        {
            using (var conn = MySqlHelper.GetConnection())
            {
                var id = MySQLExt.InsertIdentityIndex(conn, model);
                return id;
            }
        }

        public static int Update(PaperModel model)
        {
            using (var conn = MySqlHelper.GetConnection())
            {
                var num = MySQLExt.UpdateById(conn, model);
                return num;
            }
        }

        public static List<PaperModel> GetAll()
        {
            using (var conn = MySqlHelper.GetConnection())
            {
                var list = new List<PaperModel>();
                list = MySQLExt.GetAll<PaperModel>(conn).ToList();
                return list;
            }
        }

        public static List<PaperModel> GetByLevel(string level)
        {
            using(var conn = MySqlHelper.GetConnection())
            {
                string where = $"WHERE Level = {level}";
                return MySQLExt.GetByWhere<PaperModel>(conn, where).ToList();
            }
        }

        public static int Delete(int id)
        {
            using(var conn = MySqlHelper.GetConnection())
            {
                return MySQLExt.DeleteById<PaperModel>(conn, id);
                
            }
        }

        /// <summary>
        /// 获取指定的试卷
        /// </summary>
        /// <returns></returns>
        public static PaperModel GetMyPaperModel(int MyPaperId)
        {
            PaperModel paperModel = new PaperModel();
            string where = "where Id={0}";
            where = string.Format(where, MyPaperId);
            using (var conn = MySqlHelper.GetConnection())
            {
                paperModel = MySQLExt.GetByWhereFirst<PaperModel>(conn, where);
                if (paperModel == null)
                    return null;
                return paperModel;
            }
        }
    }
}
